package com.tj;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;

import com.baidu.autoupdatesdk.BDAutoUpdateSDK;
import com.baidu.autoupdatesdk.UICheckUpdateCallback;
import com.baidu.soleagencysdk.api.CheckCompletion;
import com.baidu.soleagencysdk.api.SoleAgencySDK;
import com.qq.e.ads.interstitial.AbstractInterstitialADListener;
import com.qq.e.ads.interstitial.InterstitialAD;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

@SuppressLint("NewApi")
public class MainActivity extends UnityPlayerActivity{
	private ProgressDialog dialog;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		Log.i("Baidu ","YXC"+"  开始mainActivity  ");
		//--------独家--------//
		 SoleAgencySDK.version();
		SoleAgencySDK.startToCheckShouzhu(this, new CheckCompletion() {
            @Override
            public void checkDidComplete() {
            	Log.i("Baidu ","YXC"+"  独家检查完毕,开始app后续逻辑  ");
            	
//                new AlertDialog.Builder(MainActivity.this).setMessage("SDK checked complete!")
//                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                dialog.dismiss();
//                            }
//                        }).create().show();
            }
        });
		
		
		//--------更新--------//
	      int appid = 9339123;
	      String appkey = "qxoAc4FXGc0B4CyixHiRDIGBPwULUs4B";
	      BDAutoUpdateSDK.uiUpdateAction(this, new MyUICheckUpdateCallback());
	      dialog = new ProgressDialog(this);
	      dialog.setIndeterminate(true);
	}
    private class MyUICheckUpdateCallback implements UICheckUpdateCallback {

        @Override
        public void onCheckComplete() {
        	Log.i("Baidu ","YXC"+"  自动更新检查完成  ");
            dialog.dismiss();
        }

    }
//	友盟回调以及初始化key
    public void InitUmengKey(String QqKey,String QqSecret,String WeixinKey,String WeixinSecret,String SinaKey,String SinaSecret) {
    	Log.e("umeng","YXC   假初始化Key");
    }
    
    @Override
    protected void onDestroy() {
        dialog.dismiss();
        super.onDestroy();
    }
    
    
    
    

	InterstitialAD iad;
	String interKey;
	String gdtAppid;
	boolean IsInterReady = false;

	

	public void Init(String gdtAppid, String interKey) {
		Log.i("AD_DEMO","YXC"+"  onADInitWith"+gdtAppid +"    "+ interKey);
		this.interKey = interKey;
		this.gdtAppid =gdtAppid;
		getIAD().setADListener(listenInterstitial);
	}
	AbstractInterstitialADListener listenInterstitial=new AbstractInterstitialADListener() {

		@Override
		public void onNoAD(int arg0) {
			Log.i("AD_DEMO", "YXC"+"  onADloadFail:" + arg0);
			IsInterReady = false;
			UnityPlayer.UnitySendMessage("TjSdk","onGdtEventReceived","LOADREWARDEDFAILED");  
		}

		@Override
		public void onADReceive() {
			Log.i("AD_DEMO", "YXC"+"  onADReceive");
			IsInterReady = true;
		}
		public void onADClosed() {
			Log.i("AD_DEMO", "YXC"+"  onADClose");
			UnityPlayer.UnitySendMessage("TjSdk","onGdtEventReceived","HIDDENREWARDED");  
		}
	};
	@SuppressLint("NewApi")
	public void GetInterstitial() {
		Log.i("AD_DEMO","YXC"+"  onADloadAds");

		this.runOnUiThread(new Runnable() 
		{
			@Override
			public void run() {
				iad.loadAD();
			}
		});
	}

	@SuppressLint("NewApi")
	public boolean IsInterstitialAvailable() {
		Log.i("AD_DEMO", "YXC"+"  onADIsLoad "+IsInterReady);
		return IsInterReady;
	}

	public void ShowInterstitial() {
		this.runOnUiThread(new Runnable() 
		{
			@Override
			public void run() {
				Log.i("AD_DEMO","YXC"+"  onADShowInterstitial");
				iad.show();

				IsInterReady = false;
			}
		});
	}

	public void closeAsPopup() {
		if (iad != null) {
			iad.closePopupWindow();
		}
	}

	private InterstitialAD getIAD() {
		if (iad == null) {
			iad = new InterstitialAD(this, gdtAppid,interKey);
		}
		return iad;
	}
}
